<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>坐上公交的最晚时间</title>
</head>
<body>
  <script>
    function fn(buses, passengers, capacity) {
      buses.sort((a, b) => a - b);
      passengers.sort((a, b) => a - b);
      let i = 0;
      let cur = capacity;
      for (let bus of buses) {
        for (cur = capacity; cur > 0 && i < passengers.length && passengers[i] <= bus; cur--, i++);
      }
      i--;
      let ans = cur > 0 ? buses[buses.length - 1] : passengers[i]
      while (passengers[i] === ans) {
        i--;
        ans--;
      }
      return ans;
    }
    console.log(fn([10, 20], [2, 17, 18, 19], 2))
  </script>
</body>
</html>